#include "stdafx.h"
#include "Button.h"
#include "..\..\..\..\EdgeGraphics\BitmapFont.h"
#include "..\..\..\..\EdgeContent\ContentManager.h"

Button::Button(const char* textureFilename, const char* text)
{
	memset(this->text, 0, 32);

	float width = 0, height = 0;
	texture = ContentManager::GetInstance()->LoadTexture(textureFilename, &width, &height);
	clientRect.setWidth(width);
	clientRect.setHeight(height);

	if (text)
		strcpy(this->text, text);
}

/*virtual*/ Button::~Button(void)
{
}

/*virtual*/ void Button::Update(float dt) /*override*/
{
	if (Input::GetInstance()->isKeyPressed(MouseButtons::Left))
	{
		Vector2 mousePos = Input::GetInstance()->getMousePosition();
		if (clientRect.Contains(mousePos))
		{
			float params[]
			{
				mousePos.x,
				mousePos.y,
			};

			OnClicked((void**)&params);
		}
	}

	UiElement::Update(dt);
}

/*virtual*/ void Button::Draw(void) /*override*/
{
	UiElement::Draw();
	if (font && text[0])
		font->Draw(text, (int)clientRect.left, (int)clientRect.right, 1.f, textColor);
	else if (bool(font) ^ bool(text[0]))
		OutputDebugStringA((string("Only font or \'text\' is inilitzed when drawing button ") + text + "\n").c_str());

}